package com.itheima.web;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.itheima.bean.Category;
import com.itheima.common.PageParam;
import com.itheima.common.R;
import com.itheima.sevrice.CategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RequestMapping("/category")
@RestController
public class CategoryController {

    @Autowired
    private CategoryService categoryService;

    /**
     * 分页查询
     * @param pageParam
     * @return
     */
    @GetMapping("/page")
    public R findPage(PageParam pageParam){
        IPage<Category> iPage = categoryService.findPage(pageParam);
        return R.success(iPage);
    }

    /**
     * 添加分类
     * @param category
     * @return
     */
    @PostMapping
    public R addCategory(@RequestBody Category category){
        int row = categoryService.addCategory(category);
        if (row>0){
            return R.success(category);
        }
        return R.error("添加菜品分类失败~！");
    }


    /**
     * 删除分类
     * @param id
     * @return
     */
    @DeleteMapping
    public R deleteCategory(Long id){
        int row = categoryService.deleteCategory(id);
        if (row>0){
            return R.success("删除分类成功！");
        }
        return R.error("删除分类失败~！");
    }

    /**
     * 修改分类
     * @param category
     * @return
     */
    @PutMapping
    public R updateCategory(@RequestBody Category category){
        int row = categoryService.updateCategory(category);
        if (row>0){
            return R.success("修改分类成功！");
        }
        return R.error("修改分类失败~！");
    }

    /**
     * 根据type 来查询分类。菜品分类或 套餐分类
     * @param type
     * @return
     */
    @GetMapping("/list")
    public R findByType(Integer type){
        List<Category> list = categoryService.findByType(type);
        return R.success(list);
    }



}
